package com.parent.framework.elasticsearch.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.parent.framework.elasticsearch.service.ElasticsearchService;
import com.parent.framework.elasticsearch.utils.ElasticsearchUtils;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yan
 * @Date 2023/9/28 0028 13:43
 * @Description
 */
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Override
    public <T> SearchHits<T> search(NativeSearchQuery nativeSearchQuery, Class<T> aclass) {
        SearchHits<T> searchHits = elasticsearchRestTemplate.search(nativeSearchQuery, aclass);
        return searchHits;
    }
    /**
     * @param aclass            ES实体
     * @param nativeSearchQuery 搜索构造
     * @param highlightFields   高亮字段
     * @param keys              需要高亮的字--集合
     * @param boolHighlight     是否开启高亮--不建议使用 会将一段中所有的关键字高亮不同于原生的首个高亮
     * @param preTag            高亮前缀
     * @param postTag           高亮后缀
     * @param <T>
     * @return
     */
    @Override
    public <T> List<T> searchList(Class<T> aclass, NativeSearchQuery nativeSearchQuery,
                                  List<String> highlightFields, List<String> keys, Boolean boolHighlight, String preTag, String postTag) {
        boolHighlight = ObjectUtil.isEmpty(boolHighlight) ? false : boolHighlight;
        SearchHits<T> searchHits = elasticsearchRestTemplate.search(nativeSearchQuery, aclass);
        List<T> regularMatterList = new ArrayList<>();

        if (CollUtil.isNotEmpty(searchHits)) {
            String finalPreTag = preTag;
            String finalPostTag = postTag;
            Boolean finalBoolHighlight = boolHighlight;
            regularMatterList = searchHits.stream().map(one -> {
                T content = one.getContent();
                if (finalBoolHighlight) {
                    try {
                        ElasticsearchUtils.appointSetHighlight(content, highlightFields, keys, finalPreTag, finalPostTag);
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
                return content;
            }).collect(Collectors.toList());


        }
        return regularMatterList;
    }

    /**
     * @param aclass            ES实体
     * @param nativeSearchQuery 搜索构造
     * @param highlightFields   高亮字段
     * @param keys              需要高亮的字--集合
     * @param boolHighlight     是否开启高亮--不建议使用 会将一段中所有的关键字高亮不同于原生的首个高亮
     * @param preTag            高亮前缀
     * @param postTag           高亮后缀
     * @param pageNumber
     * @param pageSize
     * @param <T>
     * @return
     */
  /*  @Override
    public <T> ResultPage<T> searchPage(Class<T> aclass, NativeSearchQuery nativeSearchQuery,
                                        List<String> highlightFields, List<String> keys, Boolean boolHighlight, String preTag, String postTag,
                                        Integer pageNumber, Integer pageSize) {
        boolHighlight = ObjectUtil.isEmpty(boolHighlight) ? false : boolHighlight;
        SearchHits<T> searchHits = elasticsearchRestTemplate.search(nativeSearchQuery, aclass);
        List<T> regularMatterList = new ArrayList<>();
        String finalPreTag = preTag;
        String finalPostTag = postTag;

        long totalHits = 0l;
//        Long pagesHits = 0L;
//        int pagesRe = 0;

        if (CollUtil.isNotEmpty(searchHits)) {
            Boolean finalBoolHighlight = boolHighlight;
            regularMatterList = searchHits.stream().map(one -> {
                T content = one.getContent();
                if (finalBoolHighlight) {
                    try {
                        ElasticsearchUtils.appointSetHighlight(content, highlightFields, keys, finalPreTag, finalPostTag);
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
                return content;
            }).collect(Collectors.toList());

            totalHits = searchHits.getTotalHits();
        }

        pageNumber = Integer.valueOf(0).equals(pageNumber) ? 1 : pageNumber;
        ResultPage<T> resultPage = ResultPage.resultPage(regularMatterList, pageNumber, pageSize, totalHits);
        return resultPage;
    }*/
}
